package com.offroader.share.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.media.BaseShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

public class ShareUtils {

	/**
	 * 分享到微信朋友圈
	 * 
	 * @param act
	 * @param appId
	 * @param appSecret
	 * @param title
	 * @param content
	 * @param imgUrl
	 * @param url
	 */
	public static void shareToWeiChatCircle(final Activity act, String appId, String appSecret, String title, String content, String imgUrl,
			String url) {

		//初始化
		UMWXHandler wxCircleHandler = new UMWXHandler(act, appId, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

		//设置分享内容
		BaseShareContent shareMediaContent = new CircleShareContent();
		shareMediaContent.setTitle(title);//标题
		shareMediaContent.setShareContent(content);//内容
		shareMediaContent.setShareImage(getShareImg(act, imgUrl));//图片
		shareMediaContent.setTargetUrl(url);//跳转URL

		//执行分享服务
		UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		mController.getConfig().closeToast();
		mController.setShareMedia(shareMediaContent);
		mController.postShare(act, SHARE_MEDIA.WEIXIN_CIRCLE, new SnsPostListener() {

			@Override
			public void onStart() {
			}

			@Override
			public void onComplete(SHARE_MEDIA shareMedia, int code, SocializeEntity entity) {
				//				if (code == 200) {
				//					Toast.makeText(act, act.getString(R.string.share_success), Toast.LENGTH_SHORT).show();
				//				} else {
				//					Toast.makeText(act, act.getString(R.string.share_fails), Toast.LENGTH_SHORT).show();
				//				}
			}
		});

	}

	/**
	 * 分享到微信
	 * 
	 * @param act
	 * @param appId
	 * @param appSecret
	 * @param title
	 * @param content
	 * @param imgUrl
	 * @param url
	 */
	public static void shareToWeiChat(final Activity act, String appId, String appSecret, String title, String content, String imgUrl, String url) {
		//初始化
		UMWXHandler wxHandler = new UMWXHandler(act, appId, appSecret);
		wxHandler.addToSocialSDK();

		//设置分享内容
		BaseShareContent shareMediaContent = new WeiXinShareContent();
		shareMediaContent.setTitle(title);//标题
		shareMediaContent.setShareContent(content);//内容
		shareMediaContent.setShareImage(getShareImg(act, imgUrl));//图片
		shareMediaContent.setTargetUrl(url);//跳转URL

		//执行分享服务
		UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		mController.getConfig().closeToast();
		mController.setShareMedia(shareMediaContent);
		mController.postShare(act, SHARE_MEDIA.WEIXIN, new SnsPostListener() {

			@Override
			public void onStart() {
			}

			@Override
			public void onComplete(SHARE_MEDIA shareMedia, int code, SocializeEntity entity) {
				//				if (code == 200) {
				//					Toast.makeText(act, "分享成功", Toast.LENGTH_SHORT).show();
				//				} else {
				//					Toast.makeText(act, "分享失败", Toast.LENGTH_SHORT).show();
				//				}
			}
		});
	}

	public static void loginWechat(final Activity act, String appId, String appSecret, final OpenLoginListener listener) {
		UMWXHandler wxHandler = new UMWXHandler(act, appId, appSecret);
		wxHandler.addToSocialSDK();

		final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
		mController.getConfig().closeToast();

		mController.doOauthVerify(act, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
			@Override
			public void onStart(SHARE_MEDIA platform) {
				Toast.makeText(act, "授权开始", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onError(SocializeException e, SHARE_MEDIA platform) {
				Toast.makeText(act, "授权错误", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onComplete(Bundle value, SHARE_MEDIA platform) {
				Toast.makeText(act, "授权完成", Toast.LENGTH_SHORT).show();

				String accessToken = value.getString("access_token");
				String openId = value.getString("openid");
				if (listener != null) {
					listener.onSuccess(accessToken, openId);
				}
			}

			@Override
			public void onCancel(SHARE_MEDIA platform) {
				Toast.makeText(act, "授权取消", Toast.LENGTH_SHORT).show();
			}
		});
	}

	private static UMImage getShareImg(Activity act, String imgUrl) {
		UMImage img = new UMImage(act, imgUrl);
		return img;
	}

}
